//按钮操作类
import Sprite from './base/sprite'
import DataBus from './databus'

let databus = new DataBus()
let diamondSize = databus.diamondSize
const ACTION_TIME_INTERVAL = 4

const Lock_IMG_SRC = 'images/lock.png'

export default class DActionItem extends Sprite {
  constructor(img,x, y,width = 3,height = 3,action_flag) {
    super(img, x, y, width, height)

    this.action_flag = action_flag;
    //锁图图片
    this.cover_img = new Sprite(Lock_IMG_SRC, x, y, width, height)

    this.action_keep = false //是否响应操作
    this.timeFlag = 0 //每次更新的时候更新时间
    this.last_action_on_time = 0 //上一次响应时的计数时间
    this.time_interval = ACTION_TIME_INTERVAL//可配置，响应计数间隔
  }

  //监测是否点击自己
  touch(x,y){
    let maxX = this.x + this.w
    let maxY = this.y + this.h

    let touchIn = (x >= this.x * diamondSize && x <= maxX * diamondSize && y >= this.y * diamondSize && y <= maxY * diamondSize);
    if (touchIn){
      this.action_keep = true;//开启响应操作
      this.last_action_on_time = 0
      this.timeFlag = 0
      return true
    }
    return false
  }

  //监测是否结束离开自己
  touchMove(x, y) {
    let maxX = this.x + this.w
    let maxY = this.y + this.h

    let touchOut = (
      x < this.x * diamondSize ||
      x > maxX * diamondSize ||
      y < this.y * diamondSize ||
      y > maxY * diamondSize);
    if (touchOut) {
      this.action_keep = false;//关闭响应操作,重置记录
      this.timeFlag = 0
      this.last_action_on_time = 0
    }
  }

  //监测是否结束点击自己
  touchEnd(x, y) {
    let maxX = this.x + this.w
    let maxY = this.y + this.h

    let touchOut = (x >= this.x * diamondSize && x <= maxX * diamondSize && y >= this.y * diamondSize && y <= maxY * diamondSize);
    if (touchOut) {
      this.action_keep = false;//关闭响应操作,重置记录
      this.timeFlag = 0
      this.last_action_on_time = 0
    }
  }

  update(){

    if(this.action_keep){
      this.timeFlag ++ //更新时间
      if (databus.propState === 1) //1.加速度
      {
      } else {
        if (this.timeFlag - this.last_action_on_time < this.time_interval)
          return;
      }
      
      this.last_action_on_time = this.timeFlag;

      let action_flag_temp = this.action_flag
      if (databus.propState === 3) //3.混乱控制
      {
        if (this.action_flag === 1){
          action_flag_temp = 3
        }
        if (this.action_flag === 3) {
          action_flag_temp = 1
        }
      }
      // 8.操作锁定(lock操作不允许执行: 左)
      // 9.操作锁定(lock操作不允许执行: 旋转)
      // 10.操作锁定(lock操作不允许执行: 右)
      if (databus.propState === 8 && this.action_flag === 1)
      { return; }
      if (databus.propState === 9 && this.action_flag === 2) 
      { return; }
      if (databus.propState === 10 && this.action_flag === 3) 
      { return; }

      switch (action_flag_temp) {
        case 1:
          databus.leftAction();
          break;
        case 2:
          databus.refreshAction();
          break;
        case 3:
          databus.rightAction();
          break;
      }
    }
  }

  drawToCanvas(ctx) {
    super.drawToCanvas(ctx)
    if (databus.propState === 8 && this.action_flag === 1) 
    { this.cover_img.drawToCanvas(ctx) }
    if (databus.propState === 9 && this.action_flag === 2) 
    { this.cover_img.drawToCanvas(ctx) }
    if (databus.propState === 10 && this.action_flag === 3) 
    { this.cover_img.drawToCanvas(ctx) }
  }
}